package com.eece496.Database;

import java.sql.*;
public abstract class Database {

	protected String tableName;
	protected java.sql.Connection connection = null;
	
	public void Setup(java.sql.Connection connection)
	{
		this.connection = connection;
	}
	
	public String AddQuotes(String s)
	{
		String result = "'" + s + "'";
		return result;
	}
	
        //@assignable  statement stmt,sqlString;
	//@signals_only RuntimeException, IOException,ServletException;	
	public void DeleteAll() throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "DELETE FROM " + tableName;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");
	}
	
	public abstract void Insert() throws SQLException;
	public abstract void Delete(String primaryKey) throws SQLException;
	public abstract void Display() throws SQLException;

//====== Added the extra functions
	// Modify a string value, primary key is int
	protected void Modify(String value, String col, String primaryKey, int tupleID) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "UPDATE " + tableName + " SET " + col + " = " + AddQuotes(value) + " WHERE " + primaryKey + " = "+ tupleID;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");			
	}
	
	// Modify an int value, primary key is int
	protected void Modify(int value, String col, String primaryKey, int tupleID) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "UPDATE " + tableName + " SET " + col + " = " + value + " WHERE " + primaryKey + " = "+ tupleID;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");			
	}

	// Modify a float value, primary key is int
	protected void Modify(float value, String col, String primaryKey, int tupleID) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "UPDATE " + tableName + " SET " + col + " = " + value + " WHERE " + primaryKey + " = "+ tupleID;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");			
	}
	
	// Modify a string value, primary key is string
	//@assignable  statement stmt,sqlString;
	//@signals_only RuntimeException, IOException,ServletException;
	protected void Modify(String value, String col, String primaryKey, String tupleID) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "UPDATE " + tableName + " SET " + col + " = " + AddQuotes(value) + " WHERE " + primaryKey + " = "+ AddQuotes(tupleID);
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");			
	}
	
	// Modify an int value, primary key is string
	//@assignable  statement stmt,sqlString;
	//@signals_only RuntimeException, IOException,ServletException;
	protected void Modify(int value, String col, String primaryKey, String tupleID) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "UPDATE " + tableName + " SET " + col + " = " + value + " WHERE " + primaryKey + " = "+ AddQuotes(tupleID);
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");			
	}

	// Modify a float value, primary key is string
	//@assignable  statement stmt,sqlString;
	//@signals_only RuntimeException, IOException,ServletException;
	protected void Modify(float value, String col, String primaryKey, String tupleID) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "UPDATE " + tableName + " SET " + col + " = " + value + " WHERE " + primaryKey + " = "+ AddQuotes(tupleID);
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");			
	}
	
}